package au.com.nh;

import java.io.IOException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import au.com.nh.dao.EMF;

import com.google.gson.Gson;

@SuppressWarnings("serial")
public class NhServlet extends HttpServlet {
	@Override
	public void doGet(final HttpServletRequest req,
			final HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		final Gson gson = new Gson();
		final Person person = new Person("Vikkus", 29);

		final EntityManager em = EMF.get().createEntityManager();
		em.persist(person);
		em.close();

		final EntityManager em1 = EMF.get().createEntityManager();
		final Query q = em1.createQuery("SELECT x FROM Person x");
		final List<Person> results = q.getResultList();

		for (final Person p : results) {
			System.out.println("Person name is " + p.getName() + p.getClass());
		}
		em1.close();

		resp.getWriter().println("Hellos, world");
		resp.getWriter().println("Hellos, world  " + gson.toJson(person));
	}
}
